/* Copyright 2012 Ali H. Caliskan.  All rights reserved.

   This file is part of Sysmkr.

   Syscfg is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
	"fmt"
	"io/ioutil"
	"libmkr"
	"os"
	"path/filepath"
)

func SysCfg(name string, database string, sysfunc string,  root string) {

	vers := libmkr.GetInfoPkg(database, name)[1]
	sys := libmkr.GetInfoPkg(database, name)[4]

	if sys != "" {

		TmpFile, err := ioutil.TempFile(os.TempDir(), name + libmkr.SysFileExt)
		libmkr.CheckError(err)

		libmkr.WriteSysFile(name, sys, TmpFile.Name())

		if libmkr.ReadSysFile(TmpFile.Name(), sysfunc) == true {
			fmt.Println("     ", name + "-" + vers)
		}

		os.Chdir(libmkr.RootDir)
		args := []string{"-i", "-c", ". " + TmpFile.Name() + "; if [ \"$(type -t "+ sysfunc + ")\" = 'function' ]; then " + sysfunc + "; fi"}
		libmkr.Run("bash", args...)
		os.Chdir("../")

		os.Remove(TmpFile.Name())
	}
}


func main() {

	flag := libmkr.GetArg(0)
	name := libmkr.GetArg(1)
	root := libmkr.GetArg(2)

	if root != "" {
		libmkr.RootDir = root
	}

	if name == "" {
		fmt.Println("warning: missing flag")
		os.Exit(0)
	}

	switch flag {
	case "free-prepare":
		fmt.Println("info: preparing package(s):")

		libmkr.LocalSQLite = filepath.Join(libmkr.RootDir, libmkr.LocalSQLite)

		for pkgname, pkggrp := range libmkr.GetPkgInfo("grp") {

			if name == pkggrp {

				SysCfg(pkgname, libmkr.LocalSQLite,
							libmkr.PkgFree, libmkr.RootDir)
			}
		}

		fmt.Println("      done")

	case "free-release":
		fmt.Println("info: releasing package(s):")

		libmkr.LocalSQLite = filepath.Join(libmkr.RootDir, libmkr.LocalSQLite)

		for pkgname, pkggrp := range libmkr.GetPkgInfo("grp") {

			if name == pkggrp {

				SysCfg(pkgname, libmkr.LocalSQLite,
							libmkr.PkgFreeClean, libmkr.RootDir)

				libmkr.EraseDataBaseEntry("pkg", libmkr.LocalSQLite, pkgname)
			}
		}

		fmt.Println("      done")

	case "sync-prepare":
		fmt.Println("info: preparing package(s):")

		for pkgname, pkggrp := range libmkr.GetPkgInfo("grp") {

			if name == pkggrp {

				SysCfg(pkgname, libmkr.GlobalSQLite,
							libmkr.PkgSyncSetup, libmkr.RootDir)
			}
		}

		fmt.Println("      done")

	case "sync-arrange":
		fmt.Println("info: arranging package(s):")

		for pkgname, pkggrp := range libmkr.GetPkgInfo("grp") {

			if name == pkggrp {

				SysCfg(pkgname, libmkr.GlobalSQLite,
							libmkr.PkgSync, libmkr.RootDir)
			}
		}

		fmt.Println("      done")

	case "":
		fmt.Println("warning: missing flag")
	}
}
